﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.Object;
using Model.DataContext;

namespace Nongsan.Controllers
{
    public class HinhAnhController : Controller
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /HinhAnh/
       public ActionResult jsonList(int idsanpham)
        {
            
            var list = from n in db.HinhAnhs where n.ID_SanPham==idsanpham select new {n.ID,n.ImagePath,n.SanPham.Ten,n.LoaiHinhAnh.Tenloai};
            return Json(list, JsonRequestBehavior.AllowGet);

        } 
		
        public ActionResult Index(int idsanpham)
        {
           
            ViewBag.idsanpham = idsanpham;
            return View();
        }

        //
        // GET: /HinhAnh/Details/5

        public ActionResult Details(int id = 0)
        {
            HinhAnh hinhanh = db.HinhAnhs.Find(id);
            if (hinhanh == null)
            {
                return HttpNotFound();
            }
            return View(hinhanh);
        }

        //
        // GET: /HinhAnh/Create

        public ActionResult Create(int idsanpham)
        {

            ViewBag.tensanpham = db.SanPhams.Find(idsanpham).Ten;
            ViewBag.idsanpham = idsanpham;
            ViewBag.ID_SanPham = new SelectList(db.SanPhams, "ID", "Ten");
            ViewBag.ID_LoaiHinhAnh = new SelectList(db.LoaiHinhAnhs, "ID", "Tenloai");
            return View();
        }

        //
        // POST: /HinhAnh/Create
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(HinhAnh hinhanh)
        {
            if (ModelState.IsValid)
            {
                db.HinhAnhs.Add(hinhanh);
                db.SaveChanges();
                return RedirectToAction("Index", new { idsanpham = hinhanh.ID_SanPham });
            }

            ViewBag.ID_SanPham = new SelectList(db.SanPhams, "ID", "Ten", hinhanh.ID_SanPham);
            ViewBag.ID_LoaiHinhAnh = new SelectList(db.LoaiHinhAnhs, "ID", "Tenloai", hinhanh.ID_LoaiHinhAnh);
            return View(hinhanh);
        }

        //
        // GET: /HinhAnh/Edit/5

        public ActionResult Edit(int id = 0)
        {
            HinhAnh hinhanh = db.HinhAnhs.Find(id);
            if (hinhanh == null)
            {
                return HttpNotFound();
            }
            ViewBag.ID_SanPham = new SelectList(db.SanPhams, "ID", "Ten", hinhanh.ID_SanPham);
            ViewBag.ID_LoaiHinhAnh = new SelectList(db.LoaiHinhAnhs, "ID", "Tenloai", hinhanh.ID_LoaiHinhAnh);
            return View(hinhanh);
        }

        //
        // POST: /HinhAnh/Edit/5
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(HinhAnh hinhanh)
        {
            if (ModelState.IsValid)
            {
                db.Entry(hinhanh).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { idsanpham = hinhanh.ID_SanPham });
            }
            ViewBag.ID_SanPham = new SelectList(db.SanPhams, "ID", "Ten", hinhanh.ID_SanPham);
            ViewBag.ID_LoaiHinhAnh = new SelectList(db.LoaiHinhAnhs, "ID", "Tenloai", hinhanh.ID_LoaiHinhAnh);
            return View(hinhanh);
        }

        //
        // GET: /HinhAnh/Delete/5

        public ActionResult Delete(int id = 0)
        {
            HinhAnh hinhanh = db.HinhAnhs.Find(id);
            if (hinhanh == null)
            {
                return HttpNotFound();
            }
            return View(hinhanh);
        }

        //
        // POST: /HinhAnh/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            HinhAnh hinhanh = db.HinhAnhs.Find(id);
            db.HinhAnhs.Remove(hinhanh);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
		[HttpPost]
        public ActionResult ajDelete(int id)
        {
             HinhAnh hinhanh = db.HinhAnhs.Find(id);
			 if (hinhanh == null)
            {
                return Json(new { success = false ,msg="Không tìm thấy đối tượng"});
            }

         
            db.HinhAnhs.Remove(hinhanh);
			db.SaveChanges();
            return Json(new { success=true});
        }
		
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}